#include <buola/scene/cskeleton.h>
#include <buola/scene/cbone.h>
#include <buola/scene/crendercontext.h>
#include <buola/scene/techniques/cskintechnique.h>

namespace buola { namespace scene {

CSkeleton::CSkeleton()
    :   mRoot(nullptr)
    ,   mTechnique(CSkinTechnique::Get())
{
    
}

CSkeleton::~CSkeleton()
{
    
}

void CSkeleton::Update()
{
    
}

void CSkeleton::Render(CRenderContext &pContext)
{
    if(!mRoot) return;

    mTechnique->Prepare(pContext);

    mMesh->Render();
}

void CSkeleton::SetRoot(PBone pBone)
{
    mRoot=pBone;
}

void CSkeleton::Prepare(PMesh pMesh)
{
    mMesh=pMesh;

    SAssignIndexContext lContext;
    lContext.mNextIndex=0;
    lContext.mMesh=mMesh;
    
    mRoot->AssignIndex(lContext);

    SSkinMeshContext lSMContext;
    lSMContext.mMesh=mMesh;

    mRoot->SkinMesh(lSMContext);
    ///\todo this thing
    //mMesh->SmoothSkinning();

    mBoneIndexCount=lContext.mNextIndex;
}

/*namespace gl*/ } /*namespace buola*/ }
